<script type="text/javascript" src="<?php echo URL_JS; ?>ckeditor/ckeditor.js"></script>

<?php

// Si le fichier de qui definie les constantes n'a pas encore ete appele on le charge
if (!defined('CONFIGURATION_FILE_LOADED'))
    include( 'includes/configuration_include.php');

include( PATH_MODELS . 'update_home_model.php');
include( PATH_VIEWS . 'update_home_view.php');

$bdd = model_db_connexion();
// Si l'utilisateur est deja logue
if (!empty($_SESSION['id_user'])) {


    echo'<span class="template_title">Gestion du message d\'acceuil</span>
               <span class="template_box_admin">';

    if (isset($_POST['new_home_message']) || isset($_POST['photo']) ) {


   
                        $type_img = upload_photo_test();
                        upload_photo($type_img);
                     echo $type_img;
        //On propose de changer le message d'acceuil
        $new_home_message = $_POST["new_home_message"];
        $req = selectMessage($bdd);
        UpdateMessage($bdd, $new_home_message,$type_img);

        viewCreated($new_home_message);


        while ($donnees = $req->fetch(PDO::FETCH_ASSOC)) {
            $home_message = $donnees['home_message'];
        printNewMessage($home_message);

        }    } else {
        //On affiche le message d'acceuil actuel
        $req = selectMessage($bdd);

        while ($donnees = $req->fetch(PDO::FETCH_ASSOC)) {
            $home_message = $donnees['home_message'];
        printNewMessage($home_message);

        }
    }
    echo '</span>';
} else {
    echo '<span class="template_span">' . LANG_NO_CONNECT . '</span>';
}




 function upload_photo( $type_img) {

        switch ($type_img) {
            case 'png' :
                if (file_exists('design/img/home .png') == true) {
                    unlink('design/img/home.png');
                }
                break;
            case 'jpg' :
                if (file_exists('design/img/home.jpg') == true) {
                    unlink('design/img/news/home.jpg');
                }

                break;
            case 'jpeg' :
                if (file_exists('design/img/news/home.jpeg') == true) {
                    unlink('design/img/news/home.jpeg')
                    ;
                }
                break;
        }

        if ($_FILES['photo']['size'] <= 7000000) {

            $infosfichier = pathinfo($_FILES['photo']['name']);

            $extension_upload = $infosfichier['extension'];

            $extensions_autorisees = array('JPG', 'jpeg', 'png', 'jpg');

            if (in_array($extension_upload, $extensions_autorisees)) {





                move_uploaded_file($_FILES['photo']['tmp_name'], "design/img/home." . $extension_upload . "");
            } else {
                return false;
            }
        } else {
            echo '<span class="template_span>"Votre photo est trop lourde</span>';
        }
    }

    function upload_photo_test() {




        $infosfichier = pathinfo($_FILES['photo']['name']);

        $extension_upload = $infosfichier['extension'];

        $extensions_autorisees = array('JPG', 'jpg', 'jpeg', 'png');

        if (in_array($extension_upload, $extensions_autorisees)) {




            switch ($extension_upload) {
                case 'png' :
                    return 0;
                    break;
                case 'jpg' :
                    return 1;
                    break;
                case 'jpeg' :
                    return 2;
                    break;
            }
            //$name= $id_work .".". $extension_upload;
        } else {
            return 3;
        }
    }
    echo'<script type="text/javascript">
       CKEDITOR.replace("visu", 
    {
    height:"200", width:"800"
    });

    </script>';
?>
